package bb.lanxing.device.sgdevcie.setting

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import bb.lanxing.R
import bb.lanxing.databinding.ActivityXossDeviceSgsettingEntryBinding
import bb.lanxing.device.sgdevcie.viewmodel.XossDeviceSGSettingEntryViewModel
import bb.lanxing.devicelist.XZDeviceListViewModel
import bb.lanxing.lib.devices.api.SmartDevice
import bb.lanxing.lib.devices.sprint.utils.XossDeviceConnectionWatcher
import bb.lanxing.mvvm.base.BaseDBActivity
import kotlin.jvm.internal.DefaultConstructorMarker
import kotlin.jvm.internal.Intrinsics
import android.util.Log;
class XossDeviceSGSettingEntryActivity :
    BaseDBActivity<ActivityXossDeviceSgsettingEntryBinding>() {

    private val viewModel: XossDeviceSGSettingEntryViewModel by lazy {
        ViewModelProvider(this)[XossDeviceSGSettingEntryViewModel::class.java]
    }

    override fun registerMessageEvent(): Boolean {
        return true
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_xoss_device_sgsetting_entry
    }

    override fun initView(t: ActivityXossDeviceSgsettingEntryBinding?) {

        var intent: Intent? = null
        var intent2: Intent? = null
        setupActionBar(true)
        val stringExtra = getIntent().getStringExtra("EXTRA_DEVICE_ADDRESS")
        Log.d("wangsm_bt", " XossDeviceSGSettingEntry")
        val activity = activity
        var str: String? = null
        val deviceType = if (activity == null || activity.intent.also {
                intent2 = it
            } == null) null else intent2?.getIntExtra(
            "EXTRA_DEVICE_TYPE",
            SmartDevice.TYPE_XOSS_NAV
        )
        val activity2 = getActivity()
        if (activity2 != null && activity2.intent.also { intent = it } != null) {
            str = intent?.getStringExtra("EXTRA_DEVICE_NAME")
        }
        viewModel.address = stringExtra
        viewModel.deviceType = deviceType
        viewModel.deviceName = str
        binding?.flSave?.setOnClickListener { view: View? ->
            if (binding?.viewDot?.visibility == View.VISIBLE) {
                sendSaveFileMsg()
            }
        }
        lifecycle.addObserver(XossDeviceConnectionWatcher(stringExtra, initSettingsView(this)))
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressed()
            return true
        }
        return true
    }

    private fun sendSaveFileMsg() {
        sendMessage(R.id.msg_save_setting_file, null as Bundle?)
    }

    override fun onMessage(i: Int, i2: Int, i3: Int, obj: Any, bundle: Bundle): Boolean {
        when (i) {
            R.id.msg_change_title -> {
                title = obj as String
            }

            R.id.msg_dismiss_save -> {
                binding!!.flSave.visibility = View.GONE
            }

            R.id.msg_dismiss_save_dot -> {
                binding!!.viewDot.visibility = View.GONE
            }

            R.id.msg_show_save -> {
                binding!!.flSave.visibility = View.VISIBLE
            }

            R.id.msg_show_save_dot -> {
                binding!!.viewDot.visibility = View.VISIBLE
            }

            else -> {
                return false
            }
        }
        return true
    }

    internal inner class initSettingsView(val settingEntry: XossDeviceSGSettingEntryActivity) :
        XossDeviceConnectionWatcher.ConnectionListener {
        override fun onConnected() {}
        override fun onConnecting() {}
        override fun onDisconnected() {
            runOnUiThread { finish() }
        }

        override fun onConnectionStateChanged(smartDevice: SmartDevice, i: Int, i2: Int) {}
    }
}
